package org.urchr.beorganized.mobile.pages.secured.agenda;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.urchr.beorganized.mobile.base.components.link.MobileLink;
import org.urchr.beorganized.mobile.base.network.NetworkException;
import org.urchr.beorganized.mobile.base.network.XMLRequest;
import org.urchr.beorganized.mobile.pages.secured.base.SecuredPage;
import org.urchr.beorganized.mobile.pages.secured.browse.BrowsePage;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

class CheckboxModel implements IModel<Boolean>{

	private boolean value = false;
	private String id = null;
	
	public CheckboxModel(String id) {
		this.id = id;
	}
	
	private static final long serialVersionUID = 5974150657223461092L;

	public Boolean getObject() {
		return value;
	}

	public void setObject(Boolean arg0) {
		value = arg0;
	}

	public void detach() {
	}
	
	public String getId(){
		return id;
	}
}

public class AgendaPage extends SecuredPage {
	
	List<CheckBox> checks = new ArrayList<CheckBox>();
	
	public AgendaPage(PageParameters parameters) {
		super(parameters);
		final int noteID = parameters.getAsInteger("id", -1);
		add(new MobileLink("back", "action/top.png", 
				new BookmarkablePageLink("clink", BrowsePage.class, 
						new PageParameters("id="+noteID)), "Go back"));
		Form form = new Form("form"){

			private static final long serialVersionUID = -3663724888002698170L;

			@Override
			protected void onSubmit() {
				resetError();
				String ids = "";
				for (CheckBox box : checks) {
					if(box.getModelObject()){
						if(!"".equals(ids))
							ids += ",";
						ids += ((CheckboxModel)box.getModel()).getId();
//						System.out.println(box.getValue()+", "+box.getModelObject()+", "+((CheckboxModel)box.getModel()).getId());						
					}
				}
				checks.clear();
				if("".equals(ids))
					return;
				XMLRequest request = new XMLRequest("agenda.CompleteTasks");
				request.addParameter("noteids", ids);
				try {
					request.callOperation();
					setResponsePage(AgendaPage.class, new PageParameters("id="+noteID));
				} catch (NetworkException e) {
					e.printStackTrace();
					showError(e.getErrorString());
				}
			}
		};
		XMLRequest request = new XMLRequest("agenda.GetAgenda");
		request.addParameter("noteid", ""+noteID);
		resetError();
		try {
			Element list = request.callOperation();
			List<Element> l = new ArrayList<Element>();
			NodeList nl = list.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				if(nl.item(i) instanceof Element)
					l.add((Element) nl.item(i));
			}
			
			form.add(new ListView<Element>("items", l){

				private static final long serialVersionUID = 5303141776839673972L;

				@Override
				protected void populateItem(ListItem<Element> item) {
					Element e = item.getModelObject();
					Label label = new Label("dates", e.getAttribute("dates"));
					label.add(new SimpleAttributeModifier("class", "urgent"+e.getAttribute("urgent")));
					
					MobileLink link = new MobileLink("link", "types/p"+e.getAttribute("icon")+".png", 
							e.getAttribute("add_icon"),
							new BookmarkablePageLink("clink", BrowsePage.class, new PageParameters("id="+e.getAttribute("id"))),
							e.getAttribute("text"));
					CheckBox box = new CheckBox("check", new CheckboxModel(e.getAttribute("id")));
					if(safeInt(e.getAttribute("type"), 0)==3){
						checks.add(box);
						item.add(box);
					}else{
						item.add(box.setVisible(false));
					}
					item.add(link);
					item.add(label);
				}
				
			});
			add(form.setVisible(l.size()>0));
		} catch (NetworkException e) {
			e.printStackTrace();
			showError(e.getErrorString());
			add(form.setVisible(false));
			form.add(new Label("items"));
		}
	}
}
